<template>
	<view>
		<view class="container">
			<view class="list-item" v-for="(item,index) in orderMaterialInfoList">
				<view class="img-view">
					<image :src="item.materialPic"></image>
				</view>
				<view class="right">
					<view class="name">{{item.materialName}}</view>
					<view class="type"></view>
					<view class="money">
						<cn-money :size="50" :money="getPrice(item.price)"></cn-money>
						<view>/{{item.unitName}}</view>
					</view>
				</view>
				<view class="num">x{{item.materialNum}}</view>
			</view>
		</view>
		<view class="bottom-view">
			<view class="left">
				<view class="money">合计：
					<cn-money :size="50" :money="getPrice(orderTotalAmount)"></cn-money>
				</view>
			</view>
			<view class="submit-btn" @click="toPay" v-if="status==='待支付'">支付</view>
		</view>

	</view>
</template>

<script>
	import * as serviceMaterialOrder from '../../common/api/serviceMaterialOrder.js'
	import * as wxPay from '../../common/api/wxPay.js'
	import * as loginUtils from '../../utils/loginUtils.js'
	import cnMoney from '../../components/cn-money/cn-money'
	export default {
		components: {
			cnMoney
		},
		data() {
			return {
				materialOrderNum: '',
				orderMaterialInfoList: [],
				consumableList: [],
				orderTotalAmount: 0,
				status:''
			}
		},
		onLoad(opt) {
			if (opt.mOrderNum) {
				this.materialOrderNum = opt.mOrderNum;
				this.queryOrderMaterialSet();
			}
		},
		computed: {
			totalMoney() {
				let sum = 0
				const list = this.orderMaterialInfoList || []
				list.forEach(e => {
					sum += e.inputPrice * e.materialNum
				})
				return sum
			}
		},
		onShow() {

		},
		methods: {
			queryOrderMaterialSet() {
				const param = {
					materialOrderNum: this.materialOrderNum
				}

				serviceMaterialOrder.queryMaterialOrder(param).then(res => {
					this.orderMaterialInfoList = res.data.data.materialItemSet;
					this.orderTotalAmount = res.data.data.totalAmount;
					this.status = res.data.data.status;
				}).catch(err => {
					console.log(err);
				});
			},
			toPay() {
				const param = {
					orderType: '护士上门-耗材',
					totalPrice: Number.parseFloat(this.orderTotalAmount) * 100,
					goodsName: "订单耗材",
					openId: uni.getStorageSync('openid_key'),
					orderSn: this.materialOrderNum,
				}
				wxPay.createOrder(param).then(result => {
					const data = result.data.data
					const orderInfo = {
						appid: loginUtils.APPID, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						nonceStr: data.nonceStr, // 随机字符串
						package: data.prepayId, // 固定值
						timeStamp: '' + data.timeStamp, // 时间戳（单位：秒）
						paySign: data.paySign, // 签名，这里用的 MD5/RSA 签名
						signType: 'HMAC-SHA256',
					}
					console.log('orderInfo', orderInfo);
					uni.requestPayment({
						provider: "wxpay",
						...orderInfo,
						success(res) {
							console.log('支付成功', res);
							uni.showToast({
								icon: 'success',
								title: '支付成功',
								complete() {
									setTimeout(() => {
										uni.navigateBack()
									}, 2000)
								}
							})
						},
						fail(err) {
							console.log('支付失败', err);
							uni.showToast({
								icon: 'none',
								title: '支付失败',
								complete() {

								}
							})
						}
					})
				}).catch(error => {
					console.log(error);
				})
			},
			getPrice(val) {
				if (!val) {
					return 0
				}
				return Number.parseFloat(val)
			},
		}
	}
</script>
<style>
	page {
		background: #F8F8F8;
	}
</style>
<style lang="scss" scoped>
	.container {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		padding: 24rpx;
		padding-bottom: 150rpx;
	}

	.list-item {
		width: 100%;
		box-sizing: border-box;
		padding: 24rpx;
		background: white;
		margin-bottom: 24rpx;
		display: flex;
		align-items: center;

		.img-view {
			margin-left: 10rpx;

			image {
				width: 160rpx;
				height: 160rpx;
			}
		}

		.right {
			display: flex;
			flex-direction: column;
			margin-left: 10rpx;
			flex: 1;

			.name {
				font-size: 34rpx;
				font-weight: bold;
			}

			.type {}

			.money {
				display: flex;
				align-items: flex-end;
				margin-top: 30rpx;
			}

		}

		.num {
			font-size: 24rpx;
			color: #333;
		}
	}

	.bottom-view {
		position: fixed;
		bottom: 0;
		width: 100%;
		background: white;
		display: flex;
		padding: 14rpx 30rpx;
		display: flex;
		box-sizing: border-box;
		align-items: center;
		justify-content: space-between;

		.money {
			margin-bottom: 20rpx;
			font-size: 26rpx;
			font-weight: bold;
		}

		.left {
			display: flex;
			align-items: center;
		}

		.line-btn {
			width: 250rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: solid 2rpx #2EB0D9;
			color: #2EB0D9;
			border-radius: 40rpx;
		}

		.submit-btn {
			background: #0BC5AE;
			color: white;
			width: 150rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 40rpx;
		}

	}
</style>